package com.fram.service.admin.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.android.category.response.CategoryWithChildrenResponse;
import com.fram.bean.api.project.request.CategoryRequest;
import com.fram.bean.api.project.response.CategoryResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.exception.ProValiDataException;
import com.fram.entity.ProjectCategory;
import com.fram.mapper.DistributorProjectCategoryMapper;
import com.fram.mapper.ProjectCategoryMapper;
import com.fram.service.admin.CategroyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2020年5月6日 - 下午3:45:45 @description：->
 */
@Service("categroyService")
public class CategroyServiceImpl implements CategroyService {

	@Autowired
	private ProjectCategoryMapper projectCategoryMapper;
	@Autowired
	private DistributorProjectCategoryMapper categoty;
	@Override
	public List<ProjectCategory> getProjectCategroy(Integer level, Long parentId) throws ProException {
		if (level == null) {
			level = 1;
		}
		List<ProjectCategory> list = projectCategoryMapper.list(level, parentId);
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
	@Override
	public void addProjectCategroy(ProjectCategory category) throws ProException {
		try {
			if (projectCategoryMapper.insertSelective(category) == 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		} catch (DataAccessException e) {
			throw new ProValiDataException(ErrCode.ERR_HAVE_EXISTED.getReturnCode(), ErrCode.ERR_HAVE_EXISTED.getReturnMsg());
		}
	}
	/**还不能批量删除**/
	@Override
	public void deleteProjectCategroy(List<ProjectCategory> category) throws ProAffairException {
		if (category == null || category.size() == 0) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		for (ProjectCategory c : category) {
			if (projectCategoryMapper.deleteByPrimaryKey(c.getId()) == 0) {
				throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), "删除失败");
			}
			if (1 == c.getLevel()) {// 如果是一级，删除所有
				try {
					projectCategoryMapper.deleteByParentId(c.getId());
				} catch (Exception e) {
				}
			}
		}
	}
	//针对 点击详情 展示的下拉。 类型展示 商品类型
	@Override
	public List<CategoryResponse> listFromCategory(CategoryRequest request) throws ProException {
		List<CategoryResponse> categoryResponse = categoty.selectProductCategoryList(request);
		if (categoryResponse == null || categoryResponse.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return categoryResponse;
	}

	@Override
	public List<CategoryResponse> listCategory(Long distributorId)throws ProException {
		List<CategoryResponse>  list= categoty.listCategory(distributorId);
		if(list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
	
	/**修改分类**/
	@Override
	public void updateProjectCategroy(ProjectCategory record)throws ProException {
		int in=projectCategoryMapper.updateByPrimaryKeySelective(record);
		if(in <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public PageInfo<ProjectCategory> listCategory(PageBean<ProjectCategory> request)throws ProException{
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<ProjectCategory> list = projectCategoryMapper.list(request.getParam().getLevel(),1L);
		if(list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public ProjectCategory details(Long id)throws ProException {
		ProjectCategory cate=projectCategoryMapper.selectByPrimaryKey(id);
		if(cate ==null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return cate;
	}

	@Override
	public void update(ProjectCategory request) throws ProException {
		if( projectCategoryMapper.updateByPrimaryKeySelective(request) == 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
	
	@Override
	public List<CategoryWithChildrenResponse> listWithChildren(Long distributorId) throws ProException {
		List<CategoryWithChildrenResponse> list=projectCategoryMapper.listWithChildren(distributorId);
		if(list == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return list;
	}
}
